package org.aftputil;

import java.io.File;

public class AFtpUtil {
	
	public static final int LINUX = 0x000001;
	public static final int WINDOWS = 0x000002;
	public static final int MAC = 0x000003;
	/**
	 * get the param of the command
	 * @param cmd the cmd to phrase
	 * @return the param
	 */
	public static String phraseCommandParam(String cmd)
	{   
		String ret = null;
		if(cmd == null || cmd.length() <=0)
			return ret;
		
		cmd = cmd.replace("\r\n"," ");
		int i = cmd.indexOf(" ");
		if(i > 0)
		{	
			ret = cmd.substring(i,cmd.length());
		    ret = ret.trim();		
		}
		return ret;
		
	}
	
	/**
	 * check if the file is Exist
	 * @param root
	 * @param curdir
	 * @return
	 */
	public static boolean fileExist(String path)
	{
		boolean ret = true;
		try
		{
		   if(!new File(path).exists())
			   return false;
		}
		catch(Exception e){e.printStackTrace();return false;}
		return ret;
		
	}
	
	/**
	 * convert the ftp path to absolute path  according to the system type
	 * @param root
	 * @param curdir
	 * @return
	 */
	public static String getAbsolutePath(int sysType  ,String root,String curdir)
	{
		String  ret = null;
		if(root == null || curdir == null)
			return ret;
		int index = 0;
		try
		{
			switch(sysType)
			{
			   case AFtpUtil.WINDOWS :
				    if(root.endsWith("\\"))
				    {
				    	index = root.lastIndexOf("\\");
				    	root = root.substring(0,index);
				    }
				    
				    curdir = curdir.replace("/", "\\");
				    break;
			   case AFtpUtil.LINUX   :
				   if(root.endsWith("/"))
				    {
				    	index = root.lastIndexOf("/");
				    	root = root.substring(0,index);
				    }
				   break;
			   case AFtpUtil.MAC     :
				    break;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		ret = root + curdir;
		return ret;	
	}
	
	public static void main(String [] args)
	{
		String root="/abc/d";
		String curdir = "/def/kkk";
		System.out.println(getAbsolutePath(AFtpUtil.LINUX,root,curdir));
			
	}
}
